package cn.kgc.crowdfundingadmin.controller;

import cn.kgc.crowdfundingadmin.common.CrowdFundingConstant;
import cn.kgc.crowdfundingadmin.common.ResultEntity;
import cn.kgc.crowdfundingadmin.pojo.Message;
import cn.kgc.crowdfundingadmin.service.MessageService;
import com.github.pagehelper.PageInfo;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author shkstart
 * @create 2020-03-01 16:33
 */
@RestController
public class MessageController {

    @Resource
    MessageService messageService;

    @RequestMapping("/select/message")
    public String selectmessage(Model model){
        List<Message> all = messageService.getAll();
      /*  model.addAttribute("list",all);*/
        model.addAttribute("list",all);
        return "message";
    }

    //分页查询用
    @RequestMapping("/message/query/for/search")
    public String queryForSearch(
            @RequestParam(value="pageNum", defaultValue="1") Integer pageNum,
            @RequestParam(value="pageSize", defaultValue="5") Integer pageSize,
            @RequestParam(value="keyword", defaultValue="") String keyword,
            Model model){
        PageInfo<Message> pageInfo = messageService.queryForKeywordSearch(pageNum, pageSize, keyword);
        model.addAttribute(CrowdFundingConstant.ATTR_NAME_PAGE_INFO);
        return "message";
    }

    @RequestMapping("/message/search/by/keyword")
    public ResultEntity<PageInfo<Message>> search(
            @RequestParam(value="pageNum", defaultValue="1",required = false) Integer pageNum,
            @RequestParam(value="pageSize", defaultValue="5",required = false) Integer pageSize,
            @RequestParam(value="keyword", defaultValue="",required = false) String keyword
    ) {
        //1.查询得到 pageInfo对象
        PageInfo<Message> pageInfo = messageService.queryForKeywordSearch(pageNum, pageSize, keyword);
        //2.封装结果对象返回
        return ResultEntity.successWithoutData(pageInfo);
    }

    //根据id查询 删除ajax的数据查询
    @ResponseBody
    @RequestMapping("/message/get/list/by/id/list")
    public ResultEntity<List<Message>> getmessageListByIdList(@RequestBody List<Integer> messageIdList) {
        List<Message> messageListByIdList = messageService.getMessageListByIdList(messageIdList);
        return ResultEntity.successWithoutData(messageListByIdList);
    }
    @ResponseBody
    @RequestMapping("/message/update/role")
    public ResultEntity<String> updateRole(Message message) {
        messageService.updateMessage(message);
        return ResultEntity.successWithoutData();
    }

}
